package cc.xiaoxiya.yy.opt.text;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import utils.FileOpt;
import utils.HttpClientUtils;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhangxb on 2017/5/15.
 */
public class OptBiQudaoCatalog implements OptStart {
    public void start(TextAction textAction) {
        try {
            String html = HttpClientUtils.doGet(textAction.getUri(), null);
            Document doc = Jsoup.parse(html);
            Element element = doc.body().select("#list").select("dl").first();
            textAction.setName(doc.title().substring(0, doc.title().indexOf("最新")));
            Elements elist = element.children();
            List<TextAction> list = new ArrayList<TextAction>();
            int dt = 0;
            String host = "http://" + new URI(textAction.getUri()).getHost();
            for (int i = 1; i < elist.size(); i++) {
                Element e1 = elist.get(i);
                if ("dt".equals(e1.nodeName())) {
                    dt++;
                } else if (dt >= 1 && "dd".equals(e1.nodeName())) {
                    Element e2 = e1.child(0);
                    list.add(new TextAction(host + e2.attr("href"), e2.html(), textAction.getDir() + textAction.getName() + "//", true));
                }
            }
            textAction.setContent(list.toString());
            textAction.setFlag(true);
            if (textAction.getFlag()) {
                FileOpt.writer(textAction.getContent(), textAction.getDir(), textAction.getName() + "-目录.json");
            }
        }catch (Exception e){

        }
    }
}
